# coding=utf-8


"""This module is a wrapper for server requests"""

from pypalo.basic import response, url, default, arguments
from pypalo.tools import tomd5

request_group="server"

def databases(sid, host=default["host"], port=default["port"], **keywords):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/databases[?PARAMETER1=value[&...]].
	
	Parameters: 
		show_normal [identifier] <Show databases of type normal (0=do not show normal databases, 1=show (default))>
		show_system [identifier] <Show databases of type system (0=do not show system databases (default), 1=show)>
		show_user_info [identifier] <Show databases of type user info (0=do not show user info databases (default), 1=show)>
		sid [string] <Session identifier for a server connection. Use the /server/login request to get a valid session identifier.>
	
	Result:
		0 database [identifier] <Identifier of the database>
		1 name_database [string] <Name of the database>
		2 number_dimensions [integer] <Number of dimensions in the database>
		3 number_cubes [integer] <Number of cubes in the database>
		4 status [integer] <Status of database (0=unloaded, 1=loaded and 2=changed)>
		5 type [integer] <Type of database (0=normal, 1=system, 3=user info)>
		6 database_token [integer] <The database token of the database>

	More info at http://host:port/api/server/databases
	'''
	#beginning of function suite
	request_name="databases"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

def info(host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/info[?PARAMETER1=value[&...]].
	
	Parameters: 
		Needs no parameter
		
	Result:
		0 major_version [integer] <major version of the server>
		1 minor_version [integer] <minor version of the server>
		2 bugfix_version [integer] <bugfix level of the server>
		3 build_number [integer] <build number of the server>
		4 encryption [integer] <0 for none, 1 for optional, 2 for required>
		5 https_port [integer] <the corresponding HTTPS port or 0 if HTTPS is not supported>

	More info at http://host:port/api/server/info
	'''
	#beginning of function suite
	request_name="info"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

def license(host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/license[?PARAMETER1=value[&...]].
	
	Parameters: 
		Needs no parameter
		
	Result:
		0 username [string] <User name>
		1 numusers [integer] <Number of users>
		2 expirationdate [integer] <Expiration date>
		3 type [integer] <License type>

	More info at http://host:port/api/server/license
	'''
	#beginning of function suite
	request_name="license"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

def load(sid, host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/load[?PARAMETER1=value[&...]].
	
	Parameters: 
		sid [string] <Session identifier for a server connection. Use the /server/login request to get a valid session identifier.>
	
	Result:
		0 OK [boolean] <"1" means OK>
	
	More info at http://host:port/server/load
	'''
	#beginning of function suite
	request_name="load"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

#NB: This function differs from the rest!!!
def login(host=default["host"], port=default["port"], **keywords):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/login[?PARAMETER1=value[&...]].
	
	Parameters:
		
		user [string] <The name of the user (required if require-user is enabled)>
		password [string] <The MD5 encrypted password of the user (required if require-user is enabled). If the supervision server is used for authentication or authorization use the extern_password parameter.>
		extern_password [string] <The plain text password of the user (required if require-user is enabled and the supervision server is used for authentication or authorization)>
	
	Result:
		0 sid [string] <Session identifier for a server connection.>
		1 ttl [string] <string 	Timeout intervall in seconds. If no request is made within this intervall the session becomes inactive.>
	
	More info at http://host:port/api/server/login
	'''
	#beginning of function suite
	request_name="login"
	parameters=arguments(locals())
	for item in parameters:
		if item=='password':
			parameters['password']=tomd5(parameters['password'])
			break
	#end of function suite
	return response(url(host, port, request_group, request_name, parameters))

def logout(sid, host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/logout[?PARAMETER1=value[&...]].
	
	Parameters: 
		sid [string] <Session identifier for a server connection. Use the /server/login request to get a valid session identifier.>
	
	Result:
		0 OK [boolean] <"1" means OK>
	
	More info at http://host:port/api/server/logout
	'''
	#beginning of function suite
	request_name="logout"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

def save(sid, host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/save[?PARAMETER1=value[&...]].
	
	Parameters: 
		sid [string] <Session identifier for a server connection. Use the /server/login request to get a valid session identifier.>
	
	Result:
		0 OK [boolean] <"1" means OK>
	
	More info at http://host:port/api/server/save
	'''
	#beginning of function suite
	request_name="save"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

def shutdown(sid, host=default["host"], port=default["port"]):
	'''
	This function wraps PALO API Request
	http://[SERVER]:[PORT]/server/shutdown[?PARAMETER1=value[&...]].
	
	Parameters: 
		sid [string] <Session identifier for a server connection. Use the /server/login request to get a valid session identifier.>
	
	Result:
		0 OK [boolean] <"1" means OK>
	
	More info at http://host:port/api/server/shutdown
	'''
	#beginning of function suite
	request_name="shutdown"
	#end of function suite
	return response(url(host, port, request_group, request_name, arguments(locals())))

# querys dictionary
query={}
query["databases"]=databases
query["info"]=info
query["license"]=license
query["login"]=login
query["logout"]=logout
query["save"]=save
query["shutdown"]=shutdown


#end of server module suite